package xin.marcher.module.attribute.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.attribute.api.AttributeApi;
import xin.marcher.module.attribute.domain.dto.*;
import xin.marcher.module.attribute.domain.request.*;
import xin.marcher.module.attribute.service.AttributeService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 属性管理相关操作API
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = AttributeApi.class, retries = 0)
public class AttributeApiImpl implements AttributeApi {

    @Resource
    private AttributeService attributeService;

    /**
     * 添加属性接口
     */
    @Override
    public JsonResult<AttributeInfoResultDTO> saveAttribute(AttributeInfoRequest request) {
        try {
            AttributeInfoResultDTO attributeResultV2DTO = attributeService.saveAttributeInfo(request);
            return JsonResult.buildSuccess(attributeResultV2DTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 给属性添加可选的属性值接口
     */
    @Override
    public JsonResult<AttributeValueResultDTO> saveAttributeValue(List<AttributeValueRequest> list) {
        try {
            AttributeValueResultDTO attributeValueResultDTO = attributeService.saveAttributeValues(list);
            return JsonResult.buildSuccess(attributeValueResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 添加属性组接口
     */
    @Override
    public JsonResult<AttributeGroupResultDTO> saveAttributeGroup(AttributeGroupRequest request) {
        try {
            AttributeGroupResultDTO attributeGroupResultDTO = attributeService.saveAttributeGroup(request);
            return JsonResult.buildSuccess(attributeGroupResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 添加属性模板接口
     */
    @Override
    public JsonResult<AttributeTemplateResultDTO> saveAttributeTemplate(AttributeTemplateRequest request) {
        try {
            AttributeTemplateResultDTO attributeTemplateResultDTO = attributeService.saveAttributeTemplate(request);
            return JsonResult.buildSuccess(attributeTemplateResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 给模板添加模板内容接口
     */
    @Override
    public JsonResult<AttributeTemplateContentResultDTO> saveAttributeTemplateContents(
            List<AttributeTemplateContentRequest> list) {
        try {
            AttributeTemplateContentResultDTO attributeTemplateContentResultDTO = attributeService
                    .saveAttributeTemplateContents(list);
            return JsonResult.buildSuccess(attributeTemplateContentResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 属性分页查询接口
     */
    @Override
    public JsonResult<PageResult<AttributeInfoDTO>> pageAttributeInfo(QueryAttributeInfoRequest request) {
        try {
            PageResult<AttributeInfoDTO> pageResult = attributeService.pageAttributeInfo(request);
            return JsonResult.buildSuccess(pageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据属性id查询属性及可选的属性值
     */
    @Override
    public JsonResult<List<AttributeValueDTO>> queryAttributeValueByAttributeId(Long attributeId) {
        try {
            List<AttributeValueDTO> attributeValueDTOS = attributeService
                    .queryAttributeValueByAttributeId(attributeId);
            return JsonResult.buildSuccess(attributeValueDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: attributeId={}", attributeId, e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("biz error: attributeId={}", attributeId, e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 属性组分页查询接口
     */
    @Override
    public JsonResult<PageResult<AttributeGroupDTO>> pageAttributeGroup(QueryAttributeGroupRequest request) {
        try {
            PageResult<AttributeGroupDTO> pageResult = attributeService.pageAttributeGroup(request);
            return JsonResult.buildSuccess(pageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 属性模板分页查询接口
     */
    @Override
    public JsonResult<PageResult<AttributeTemplateDTO>> pageAttributeTemplate(QueryAttributeTemplateRequest request) {
        try {
            PageResult<AttributeTemplateDTO> pageResult = attributeService.pageAttributeTemplate(request);
            return JsonResult.buildSuccess(pageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据模板id查询模板内容接口
     */
    @Override
    public JsonResult<List<AttributeTemplateContentDTO>> queryAttributeTemplateContentByTemplateId(Long templateId) {
        try {
            List<AttributeTemplateContentDTO> attributeTemplateContentDTOS = attributeService
                    .queryAttributeTemplateContentByTemplateId(templateId);
            return JsonResult.buildSuccess(attributeTemplateContentDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: attributeId={}", templateId, e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("biz error: attributeId={}", templateId, e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
